package com.sqs.controller;


import com.sqs.constant.ResultConstant;
import com.sqs.context.BaseContext;
import com.sqs.result.Result;
import com.sqs.service.ISessionService;
import com.sqs.vo.ChatMessageVO;
import com.sqs.vo.SessionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/session")
public class SessionController {

    @Autowired
    private ISessionService iSessionService;

    /**
     * 查询用户的全部会话
     * @return
     * */
    @GetMapping
    public Result<List<SessionVO>> sessionsSelect(){
        log.info("查询用户的全部session：{}", BaseContext.getCurrentId());
        return Result.success(ResultConstant.SEARCH_SUCCESS, iSessionService.sessionsSelect());
    }

    /**
     * 创建新的会话
     * @param name
     * @return
     * */
    @PostMapping
    public Result<SessionVO> sessionInsert(String name){
        log.info("创建新的对话,name:{},userId:{}", name, BaseContext.getCurrentId());
        return Result.success(ResultConstant.ADD_SUCCESS, iSessionService.sessionInsert(name));
    }

    /**
     * 更改会话名称
     * @param id
     * @param name
     * @return
     * */
    @PutMapping
    public Result sessionUpdate(Long id, String name){
        log.info("更改会话名称, id:{}, name:{}", id, name);
        iSessionService.sessionUpdate(id, name);
        return Result.success(ResultConstant.UPDATE_SUCCESS);
    }

    /**
     * 查询会话的对话记录
     * @param id
     * @return
     * */
    @GetMapping("/chat")
    public Result<List<ChatMessageVO>> chatMessageSelect(Long id){
        log.info("查询会话的对话记录:{}", id);
        return Result.success(ResultConstant.SEARCH_SUCCESS, iSessionService.chatMessageSelect(id));
    }

}
